package com.dsmbase.corp.user.service;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dsmbase.corp.common.Code;
import com.dsmbase.corp.common.ServiceException;
import com.dsmbase.corp.user.dao.UserDao;
import com.dsmbase.corp.user.domain.User;
import com.dsmbase.corp.user.web.LoginForm;

@Component
public class UserService {

	@Autowired
	private UserDao userDao;
	
	public User verifyUserLogin(LoginForm form) throws ServiceException {
		User user = null;
		try {
			user = userDao.getUserByName(form.getUsername());
			if (!user.getUserPassword().equals(form.getPassword())) {
				throw new ServiceException(Code.ILLEGAL_PARAM, "invalid user login");
			}
		} catch (SQLException e) {
			throw new ServiceException(Code.ILLEGAL_PARAM);
		}
		return user;
	}
	
	public List<User> getAllUser() throws ServiceException {
		try {
			return userDao.getAllUser();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new ServiceException(Code.ILLEGAL_PARAM);
		}
	}
	
	public User getUserBy(int id) throws ServiceException {
		try {
			return userDao.getUserById(id);
		} catch (SQLException e) {
			// TODO log
			throw new ServiceException(Code.ILLEGAL_PARAM);
		}
	}
}
